import javax.media.opengl.GL;


public class Switcher {
	int switcher;
	float xsize=200;
	float ysize=30;
	float zsize=200;
	float xdisp=600;
	float ydisp=0;
	float zdisp=600;
	public boolean active=false;
	boolean [] pushers={false,false};
	
	

	public void buildSwitch(GL gl){
		switcher = gl.glGenLists(1);


		gl.glNewList(switcher, GL.GL_COMPILE);

		gl.glBegin(GL.GL_QUAD_STRIP);
		
			gl.glColor3f(1, 0, 0);
			gl.glVertex3d(0, 0, 0);
			gl.glVertex3d(0, ysize, 0);
	
			gl.glColor3f(0, 1, 0);
			gl.glVertex3d(xsize, 0, 0);
			gl.glVertex3d(xsize, ysize, 0);
	
			gl.glColor3f(0, 0, 1);
			gl.glVertex3d(xsize, 0, 200);
			gl.glVertex3d(xsize, ysize, zsize);
	
			gl.glColor3f(1, 1, 0);
			gl.glVertex3d(0, 0, zsize);
			gl.glVertex3d(0, ysize, zsize);
	
			gl.glColor3f(1, 0, 0);
			gl.glVertex3d(0, 0, 0);
			gl.glVertex3d(0, ysize, 0);
			gl.glVertex3d(0, ysize, 0);
			gl.glVertex3d(0, ysize, zsize);
			gl.glVertex3d(xsize, ysize, 0);
			gl.glVertex3d(xsize, ysize, zsize);
			
			gl.glColor3f(1, 1, 1);

	    gl.glEnd();

	    gl.glEndList();
	}
	public void draw(GL gl){
		
		gl.glPushMatrix();
		gl.glTranslated(xdisp,ydisp,zdisp );
		

		gl.glCallList(switcher);
		gl.glPopMatrix();
	}
	public void checkActive(Point3D loc,int objectPushingTheSwitch) {
		
		float al=0;
		
		Point3D v1 = loc.minus(new Point3D(0+xdisp,0+ydisp,0+zdisp));
		Point3D v2 = loc.minus(new Point3D(xsize+xdisp,0+ydisp,0+zdisp));
		al += v1.calculateAngle(v2);
		Point3D v3 = loc.minus(new Point3D(xsize+xdisp,0+ydisp,zsize+zdisp));
		al += v2.calculateAngle(v3);
		Point3D v4 = loc.minus(new Point3D(0+xdisp,0+ydisp,zsize+zdisp));
		al += v3.calculateAngle(v4);
		
		al += v4.calculateAngle(v1);

		if (al<359 ){
			pushers[objectPushingTheSwitch-1]=false;
			
			
		}else{
			
			pushers[objectPushingTheSwitch-1]=true;
		}
		active=false;
		for (int i = 0; i < pushers.length; i++) {
			active= active || pushers[i];
		}
		
	}
}
